package com.wanmait.houlang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.houlang.mapper.ProfessionalMapper;
import com.wanmait.houlang.pojo.DTO;
import com.wanmait.houlang.pojo.Professional;
import com.wanmait.houlang.pojo.ScCagRelation;
import com.wanmait.houlang.pojo.School;
import com.wanmait.houlang.service.ProfessionalService;
import com.wanmait.houlang.service.ScCagRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class ProfessionalServiceImpl implements ProfessionalService {
    @Autowired
    private ProfessionalMapper professionalMapper;
    @Autowired
    private ScCagRelationService scCagRelationService;

    @Override
    public void insert(DTO dto) {
        professionalMapper.insertSelective(dto);
    }

    @Override
    public void insert(Professional object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(Professional object) {

    }

    @Override
    public Professional findById(Integer id) {

        return null;
    }


    @Override
    public List<Professional> findAll() {
        List<Professional> professionals=professionalMapper.findAll();
        return professionals;
    }

    @Override
    public PageInfo<Professional> findAll(int pageNum, int pageSize, Professional object) {

        return null;
    }

    //获取学校对应的一二级分类专业
    @Override
    public List<Professional> findBySchoolId(Integer id) {
        List<Professional> professionals=professionalMapper.findBySchoolId(id);
        List<ScCagRelation> scCagRelations = scCagRelationService.findBySchoolId(id);
        for (int i = 0; i < professionals.size(); i++) {
            ArrayList<Professional> pros=new ArrayList<>();
            for (int j = 0; j <scCagRelations.size() ; j++) {
                if(professionals.get(i).getId()==scCagRelations.get(j).getProfessional().getPid()){
                    pros.add(scCagRelations.get(j).getProfessional());
                }
            }
            professionals.get(i).setProfessionalList(pros);
        }
        return professionals;
    }
//    获取所有的本科专业

    @Override
    public List<Professional> findBachelorPro() {
        List<Professional> bachelorPros=professionalMapper.findBachelorPro();
        //拼装
        List<Professional> pros = new ArrayList<>();
        //将所有的对象保存到map集合中
        HashMap<Integer,Professional> mapPros=new HashMap<>();

        for (Professional professional:bachelorPros){
            mapPros.put(professional.getId(),professional);
        }

        for(Professional professional:bachelorPros){

            if(professional.getPid()==null){
                pros.add(professional);
                continue;
            }
            //获取到当前对象的父级对象
            Professional parent = mapPros.get(professional.getPid());
            //判断当前对象的list属性是否为空
            if(parent.getProfessionalList()==null){
                parent.setProfessionalList(new ArrayList<>());
            }
            //将对象放入父级对象的list集合中
            parent.getProfessionalList().add(professional);

        }

        return pros;
    }

    @Override
    public List<Professional> findJuniorPro() {
        List<Professional> juniorPro=professionalMapper.findJuniorPro();
        //拼装
        List<Professional> junior = new ArrayList<>();
        //将所有的对象保存到map集合中
        HashMap<Integer,Professional> mapJunior=new HashMap<>();
        for (Professional professional:juniorPro){
            mapJunior.put(professional.getId(),professional);
        }
        for(Professional professional:juniorPro){
            if(professional.getPid()==null){
                junior.add(professional);
                continue;
            }
            //获取到当前对象的父级对象
            Professional parent = mapJunior.get(professional.getPid());
            //判断当前对象的list属性是否为空
            if(parent.getProfessionalList()==null){
                parent.setProfessionalList(new ArrayList<>());
            }
            //将对象放入父级对象的list集合中
            parent.getProfessionalList().add(professional);

        }
        return junior;
    }

    @Override
    public Professional majorPro(Integer id) {
        Professional schools=professionalMapper.major(id);
        return schools;
    }




}
